import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.mysql.jdbc.ResultSetMetaData;


public class TranscriptPanel extends JPanel{
	JPanel panel=new JPanel();
	JPanel center;
	JLabel transcript;
	JButton okButton;
	String[] cols={"Course/Section and Title","Grade","Credits","Repeats","Term"};
	Object data[][]={
	};
	JTable table;
	JLabel name;
	JPanel north;
	JPanel listPane;
	JPanel south;
	JLabel totalC,totalG,GPA;
	Image img;
	
	public TranscriptPanel(){
		//entire interface
		
		img=new ImageIcon("transcript_background.png").getImage();
		setOpaque(false);
		
		//make the panel to be located in the middle with graphics
			//	JPanel backPanel=new JPanel();
				//backPanel.setPreferredSize(new Dimension(600,400));
			//backPanel.setLayout(new GridBagLayout());
			//	backPanel.add(panel);
		
				//fix the panel loc change
				setLayout(new BorderLayout());
				add(panel,BorderLayout.PAGE_START);
				//add(backPanel,BorderLayout.PAGE_START);
				
		panel.setPreferredSize(new Dimension(600,400));
		panel.setLayout(new BorderLayout());
		panel.setBackground(Color.WHITE);
		panel.setOpaque(false);
		//JScrollPane scroll=new JScrollPane(panel);//,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		//scroll.setMinimumSize(new Dimension(600,400));
		//scroll.setPreferredSize(new Dimension(600,400));
		
		//initialize by getting data from database
		initialize();
		
		
		
		//north of the panel
		north=new JPanel();
		north.setBackground(Color.WHITE);
		north.setLayout(new BorderLayout());
		transcript=new JLabel("Transcript");
		//name=new JLabel("1234567"+ "Sample Name");
		
		JPanel glue=new JPanel();
		glue.setPreferredSize(new Dimension(600,15));
		glue.setBackground(Color.white);
		glue.setOpaque(false);
		
		north.add(transcript,BorderLayout.NORTH);
		north.add(glue,BorderLayout.CENTER);
		north.add(name,BorderLayout.SOUTH);
		north.setOpaque(false);
		
		panel.add(north, BorderLayout.NORTH);
		
		
		
		//center of the panel
		center=new JPanel();
		center.setBackground(Color.WHITE);
		center.setLayout(new BorderLayout());
		
		TableColumn column;
		column = table.getColumnModel().getColumn(0);
		column.setPreferredWidth(270);
		
		int v2 = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; 
		int h2 = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		table.setPreferredScrollableViewportSize(new Dimension(580, 38));
		JScrollPane j2 = new JScrollPane(table,v2,h2);
		table.setFillsViewportHeight(true);
		
		center.add(table.getTableHeader(), BorderLayout.PAGE_START);
		center.add(j2, BorderLayout.CENTER);
		
		panel.add(center,BorderLayout.CENTER);
		
		
		
		//south of the panel
		south=new JPanel();
		south.setBackground(Color.WHITE);
		south.setLayout(new BorderLayout());
				
		listPane = new JPanel();
		listPane.setBackground(Color.WHITE);
		listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
		JLabel totalCredits=new JLabel("Total Earned Credits");
		JLabel totalGrades=new JLabel("Total Grade Point");
		JLabel cumGPA=new JLabel("Cumulative GPA");
		listPane.add(totalCredits);
		listPane.add(totalGrades);
		listPane.add(cumGPA);
		
		JPanel infoPane=new JPanel();
		infoPane.setBackground(Color.WHITE);
		infoPane.setLayout(new BoxLayout(infoPane, BoxLayout.PAGE_AXIS));
		infoPane.add(totalC);
		infoPane.add(totalG);
		infoPane.add(GPA);
		
		JPanel gridPane=new JPanel();
		gridPane.setLayout(new GridLayout(1,3));
		gridPane.setBackground(Color.white);
		gridPane.add(listPane);
		gridPane.add(infoPane);
		
				
		south.add(gridPane,BorderLayout.NORTH);
				
		
		JPanel buttonPanel=new JPanel();
		buttonPanel.setOpaque(false);
		buttonPanel.setBackground(Color.white);
		buttonPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		final JButton button1=new JButton();
		final ImageIcon ho=new ImageIcon("home_button.png");
		final ImageIcon cho=new ImageIcon("home_cbutton.png");
		button(button1,ho);
		//button = new JButton("5");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 0;       //reset to default
		c.weighty = 1.0;   //request any extra vertical space
		c.anchor = GridBagConstraints.PAGE_END; //bottom of space
		c.insets = new Insets(10,0,0,0);  //top padding
		
		button1.addActionListener(new ActionListener(){
		        public void actionPerformed(ActionEvent e){
		              remove(panel);
		              add(new MenuPanel());
		              validate();
		              repaint();
		          }
		});
		button1.setContentAreaFilled(false);
		button1.setIcon(ho);
		button1.setRolloverIcon(cho);
		button1.setPressedIcon(cho);
		button1.getModel().addChangeListener(new ChangeListener() {
		        @Override
		        public void stateChanged(ChangeEvent e) {
		            ButtonModel model = (ButtonModel) e.getSource();
		            if (model.isRollover()) {
		            	button1.setIcon(cho);
		            } else {
		            	button1.setIcon(ho);
		            }// etc ...//  if (model.isPressed()) {
		         }
		    });
		buttonPanel.add(button1,c);
				
		south.setOpaque(false);
		south.add(buttonPanel,BorderLayout.SOUTH);
				
		panel.add(south,BorderLayout.SOUTH);
		
		//fix loc error
		setLayout(new BorderLayout());
		add(panel,BorderLayout.PAGE_START);
	}

	
	private void initialize(){
		//initialize table/////////
		DefaultTableModel dtm=new DefaultTableModel(data,cols){
			public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
		};
		table=new JTable(dtm);
		String sectionAndTitle,grade,term;
		int repeats;
		int credits,totalg=1;
		int row,col;//increase as item added
		float gpa=1;
		float totalc=1;
		
		//dummy values
		sectionAndTitle="CS4410-Database";
		//grade="A";
		term="14/FA";
		//repeats=0;
		credits=3;
		
		String SID=MenuPanel.SID;
        totalc=1;
        totalg=1;
       
       
        try{
                String dName = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                Connection con = DriverManager.getConnection(url);
               
                java.sql.Statement mysqlStatement2 = con.createStatement();
                java.sql.ResultSet resultSet2 = mysqlStatement2.executeQuery("SELECT grade, repeats, term FROM total_course_list WHERE SID='"+MenuPanel.SID+"'");
                java.sql.Statement mysqlStatement3 = con.createStatement();
                java.sql.ResultSet resultSet3 = mysqlStatement3.executeQuery("SELECT credit_hours, course_name, course_number FROM course_catalog WHERE CID IN (SELECT CID FROM total_course_list WHERE SID='"+MenuPanel.SID+"')");
                //java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT major, grade, credit_hours FROM course_catalog, student_list, total_course_list WHERE student_list.SID='"+MenuPanel.SID+"'");// CID IN (SELECT CID FROM total_course_list WHERE SID='"+MenuPanel.SID+"')");
                while (resultSet2.next()&&resultSet3.next()) {
                grade= resultSet2.getString(1);
                repeats=resultSet2.getInt(2);
                term=resultSet2.getString(3);
                credits=resultSet3.getInt(1);
                sectionAndTitle=resultSet3.getString(2)+" - "+resultSet3.getString(3);
               
               
                totalc+=credits;
                if(grade.equals("A")){
                        totalg+=4*credits;
                }
                else if(grade.equals("B")){
                        totalg+=3*credits;
                }
                else if(grade.equals("C")){
                        totalg+=2*credits;
                }
                else if(grade.equals("D")){
                        totalg+=1*credits;
                }
                
                System.out.println(sectionAndTitle);
    			
    			Object[] course={sectionAndTitle,grade,credits,repeats,term};
    			dtm.addRow(course);
               
                }
                con.close();
                }
                catch(Exception ex){}
		
		
		
		
		//initialize other values
		totalC=new JLabel(totalc+"");
		totalG=new JLabel(totalg+"");
		//GPA=new JLabel(totalg/totalc+"");		
		gpa=totalg/totalc;
		GPA=new JLabel(gpa+"");
		
		name=new JLabel(MenuPanel.student_id+"  "+ MenuPanel.fname+" "+MenuPanel.lname);
		
	}
	
	
	@Override
    public void paintComponent(Graphics g) {
		super.paintComponents(g);
        g.drawImage(img, 0, 0, this);
    }
	
	private void button(JButton b, ImageIcon i){
		b.setBorderPainted(false);
		b.setBorder(null);
		//button.setFocusable(false);
		b.setMargin(new Insets(0, 0, 0, 0));
		b.setContentAreaFilled(false);
		b.setIcon(i);
		//b.setRolloverIcon(myIcon2);
		//b.setPressedIcon(myIcon3);
	}
	
}
